#include "TimeOperator.h"
#include "UIUtils.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QSpinBox>
#include <QLabel>

namespace
{
const QString kCountdown = "Countdowner %0";
const QString kRemainTime = " %0 Sec";
const QString kTimeOut = "Countdown time out!";
}

struct TimeDownStruct {
    TimeDownStruct() {}
    TimeDownStruct(QCheckBox *check, QSpinBox *spin, QLabel *lab) : pCheck(check),
        pTimeSpin(spin), plabel(lab) {}
    QCheckBox *pCheck = nullptr;
    QSpinBox *pTimeSpin = nullptr;
    QLabel *plabel = nullptr;
    unsigned timeDrun = 0;
    bool bmain = false;
    void setMain(bool b) { bmain = b; }
    void start()
    {
        int durnTime = pTimeSpin->value();
        if (!durnTime) return;
        pCheck->setChecked(true);
        pCheck->setDisabled(true);
        pTimeSpin->setDisabled(true);
        plabel->setText(kRemainTime.arg(durnTime * 60));
        timeDrun = durnTime * 60;
        if (bmain) {
            Message::TrayMessage *msg = Message::TrayMessage::Ins();
            msg->startTaskbarProgressFunc(0, timeDrun);
        }
    }
    void fresh()
    {
        if (!pCheck || !pCheck->isChecked()) return;
        if (timeDrun > 0) {
            --timeDrun;
            int hr = timeDrun / 3600;
            int mi = (timeDrun % 3600) / 60;
            int se = timeDrun % 60;
            QString msg;
            if (hr > 0) msg += QString("%0H ").arg(hr);
            if (mi > 0) msg += QString("%0m ").arg(mi);
            msg += QString("%0 s").arg(se);
            plabel->setText(msg);
            if (bmain) {
                int durnTime = pTimeSpin->value();
                Message::TrayMessage *msg = Message::TrayMessage::Ins();
                msg->setTaskBarProgressValueFunc(durnTime * 60 - timeDrun);;
            }
            if (!timeDrun) {
                stop();
                if (Message::TrayMessage *msg = Message::TrayMessage::Ins())
                    msg->showMsg(pCheck->text(), kTimeOut, QIcon(), 60000);
            }
        }
    }
    void stop()
    {
        pCheck->setChecked(false);
        pCheck->setDisabled(false);
        pTimeSpin->setDisabled(false);
        plabel->setText(kRemainTime.arg(0));
        timeDrun = 0;
    }
};

TimeOperWidget::TimeOperWidget(QWidget *par): QWidget(par)
{
    _timer.setInterval(1000);
    _timer.start();
    QObject::connect(&_timer, &QTimer::timeout, [ = ]() {
        for (auto &devTm : _timedownStructs)
            devTm.fresh();
    });
    QVBoxLayout *mainLay = new QVBoxLayout;
    mainLay->addLayout(newCountdownWdg(), 10);
    mainLay->addLayout(newCountdownWdg(), 10);
    mainLay->addStretch();
    setLayout(mainLay);
}

TimeOperWidget::~TimeOperWidget()
{
    _timer.stop();
}

void TimeOperWidget::startCountdown()
{

}
QBoxLayout *TimeOperWidget::newCountdownWdg()
{
    static int idx = 0;
    if (idx > 10) return nullptr;
    QHBoxLayout *hLay = new QHBoxLayout();
    QCheckBox *pCheck = new QCheckBox(kCountdown.arg(++idx), this);
    pCheck->setCheckable(true);
    hLay->addWidget(pCheck, 5);
    QSpinBox *pTimeDure = new QSpinBox(this);
    pTimeDure->setSuffix(" Minutes");
    pTimeDure->setRange(0, 180);
    pTimeDure->setSingleStep(5);
    pTimeDure->setWrapping(true);
    pTimeDure->setReadOnly(false);
    hLay->addWidget(pTimeDure, 15);
    QLabel *plabel = new QLabel("0 Sec");
    hLay->addWidget(plabel, 10);
    QPushButton *startBtn = new QPushButton("Start", this);
    hLay->addWidget(startBtn, 5);
    QPushButton *stopBtn = new QPushButton("Stop", this);
    stopBtn->setDisabled(true);
    hLay->addWidget(stopBtn, 5);
    _timedownStructs.insert(pCheck->text(), TimeDownStruct(pCheck, pTimeDure,
                                                           plabel));
    if (_timedownStructs.size() == 1) _timedownStructs[pCheck->text()].setMain(
            true);
    QObject::connect(startBtn, &QPushButton::clicked, [ = ]() {
        if (_timedownStructs.contains(pCheck->text())) {
            _timedownStructs[pCheck->text()].start();
            startBtn->setDisabled(true);
            stopBtn->setDisabled(false);
        }
    });
    QObject::connect(stopBtn, &QPushButton::clicked, [ = ]() {
        if (_timedownStructs.contains(pCheck->text())) {
            _timedownStructs[pCheck->text()].stop();
            startBtn->setDisabled(false);
            stopBtn->setDisabled(true);
        }
    });
    return hLay;
}